home *** CD-ROM | disk | FTP | other *** search
/ Zoom 2 / Zoom - Release 2 (1996)(Active Software)[!].iso / programming / amiga / muibuilder / mb / developer / c / sources_gencodec / tools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-17  |  11.6 KB  |  438 lines

  1. #include <clib/exec_protos.h>
  2. #include <clib/dos_protos.h>
  3. #include <clib/intuition_protos.h>
  4. #include <exec/memory.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8. #include "Tools.h"
  9.  
  10. extern void Quit(void);
  11.  
  12. struct MemNode
  13. {
  14.   struct MemNode *child;             /* Next block in list        */
  15.  
  16.   char *file;                        /* File that allocated us    */
  17.   ULONG line;                        /* Line we were allocated on */
  18.  
  19.   ULONG size;                        /* Size of memory block      */
  20. };
  21.  
  22. static struct MemNode *head = NULL;
  23.  
  24. /****************************************************************************************************************/
  25. /****                                                                                                         ****/
  26. /**                                            DisplayMsg                                                           **/
  27. /****                                                                                                         ****/
  28. /****************************************************************************************************************/
  29.  
  30. void DisplayMsg(char *Msg)
  31. {
  32.     struct EasyStruct RequestMsg =
  33.     {
  34.         sizeof(struct EasyStruct),
  35.         0,
  36.         "GenCode C Error",
  37.         "%s",
  38.         "OK"
  39.     };
  40.  
  41.     EasyRequest(NULL,&RequestMsg,NULL,Msg);
  42. }
  43.  
  44. /****************************************************************************************************************/
  45. /****                                                                                                         ****/
  46. /**                                            safe_AllocMemory                                                   **/
  47. /****                                                                                                         ****/
  48. /****************************************************************************************************************/
  49.  
  50. void *safe_AllocMemory(ULONG byteSize,char *File,ULONG Line)
  51. {
  52.     static char msg[80];
  53.     struct MemNode *t;
  54.  
  55.     if (head)
  56.         t = head;
  57.     else
  58.         t = NULL;
  59.  
  60.     if (!(head = AllocVec(byteSize + sizeof(struct MemNode),MEMF_PUBLIC|MEMF_CLEAR)))
  61.     {
  62.         sprintf(msg,"ERROR !!!!! NOT ENOUGH MEMORY !!!\nCan't allocate %ld bytes\n",byteSize);
  63.         DisplayMsg(msg);
  64.         return NULL;
  65.     }
  66.  
  67.     head->child = t;
  68.     head->file = File;
  69.     head->line = Line;
  70.     head->size = byteSize;
  71.  
  72.     return ((void *)((ULONG)head + sizeof(struct MemNode)));
  73. }
  74.  
  75. /****************************************************************************************************************/
  76. /****                                                                                                         ****/
  77. /**                                            safe_FreeMemory                                                       **/
  78. /****                                                                                                         ****/
  79. /****************************************************************************************************************/
  80.  
  81. void safe_FreeMemory(void *ptr,char* File,ULONG Line)
  82. {
  83.     static char msg[81];
  84.     struct MemNode *t1,*t2;
  85.  
  86.     if (ptr == NULL)
  87.         return;
  88.  
  89.     t1 = head;
  90.     t2 = NULL;
  91.  
  92.     while(t1)
  93.     {
  94.         if((ULONG)ptr == (ULONG)t1 + sizeof(struct MemNode))
  95.         {
  96.             if(t2)
  97.                 t2->child = t1->child; /* Remove block from the list */
  98.             else
  99.                 head = t1->child;
  100.  
  101.             FreeVec(t1);
  102.             return;
  103.         }
  104.         t2 = t1;
  105.         t1 = t1->child;
  106.       }
  107.  
  108.     sprintf(msg,"%s %ld: Free twice (?) FreeVec($%lx)",File,Line,(ULONG)ptr - sizeof(struct MemNode));
  109.     DisplayMsg(msg);
  110.     safe_ShowMemory(File,Line);
  111. }
  112.  
  113. /****************************************************************************************************************/
  114. /****                                                                                                         ****/
  115. /**                                            safe_ShowMemory                                                       **/
  116. /****                                                                                                         ****/
  117. /****************************************************************************************************************/
  118.  
  119. void safe_ShowMemory(char* File,ULONG Line)
  120. {
  121.     struct MemNode *t;
  122.     BPTR con;
  123.  
  124.     if(head)
  125.     {
  126.         if (!(con = Open("CON:0/10/400/100/ShowMemory .../AUTO/CLOSE/WAIT/INACTIVE",MODE_NEWFILE)))
  127.             return;
  128.  
  129.         t = head;
  130.         FPrintf(con, "%s %ld: Memory List\n", File, Line);
  131.  
  132.         FPrintf(con,"    %-12s %-12s %-16s %-4s\n","Address", "Size", "File", "Line");
  133.         FPrintf(con,"    %-12s %-12s %-16s %-4s\n","-------", "----", "----", "----");
  134.  
  135.         while(t)
  136.         {
  137.             FPrintf(con,"    $%-11lx %-12ld %-16s %-4ld\n",t,t->size,t->file,t->line);
  138.             t = t->child;
  139.         }
  140.     }
  141. }
  142.  
  143. /****************************************************************************************************************/
  144. /****                                                                                                         ****/
  145. /**                                            safe_ClearMemory                                                   **/
  146. /****                                                                                                         ****/
  147. /****************************************************************************************************************/
  148.  
  149. void safe_ClearMemory(char* File,ULONG Line)
  150. {
  151.     static char msg[210];
  152.     struct MemNode *t1, *t2;
  153.  
  154.     if(head)
  155.     {
  156.         t1 = head;
  157.         sprintf(msg, "Oups !! there is a BUG (a memory allocation problem)\nPlease report this bug to GenCode'sAuthors\nwith the next parameters (in ShowMemory Window)\n%s %ld: Freeing Memory List", File, Line);
  158.         DisplayMsg(msg);
  159.         safe_ShowMemory(File,Line);
  160.  
  161.         while(t1)
  162.         {
  163.             t2 = t1->child;
  164.             FreeVec(t1);
  165.             t1 = t2;
  166.         }
  167.     }
  168.  
  169.     head = NULL;
  170. }
  171.  
  172. /****************************************************************************************************************/
  173. /****                                                                                                         ****/
  174. /**                                            CopyBlock                                                           **/
  175. /****                                                                                                         ****/
  176. /****************************************************************************************************************/
  177.  
  178. char *CopyBlock(FILE *file,char *Filechar,char *String,
  179.                 char *begin,char *end,
  180.                 char *MsgErrorBegin,char *MsgErrorEnd,
  181.                 char *MainFile)
  182. {
  183.     char ctmp;
  184.     char *str,*str1;
  185.  
  186.     if (!(str = strstr(String,begin)))
  187.     {
  188.         char *msg;
  189.  
  190.         if (!(msg = (char *)AllocMemory(80+strlen(MsgErrorBegin)+strlen(MainFile))))
  191.         {
  192.             fclose(file);
  193.             FreeMemory(Filechar);
  194.             Quit();
  195.         }
  196.         sprintf(msg,"Can't find the next line in old file %s !!! :\n \"%s\"",MainFile,MsgErrorBegin);
  197.         DisplayMsg(msg);
  198.         FreeMemory(msg);
  199.         fclose(file);
  200.         FreeMemory(Filechar);
  201.         Quit();
  202.     }
  203.     if (end)
  204.     {
  205.         if (!(str1 = strstr(str,end)))
  206.         {
  207.             char *msg;
  208.  
  209.             if (!(msg = (char *)AllocMemory(80+strlen(MsgErrorEnd)+strlen(MainFile))))
  210.             {
  211.                 fclose(file);
  212.                 FreeMemory(Filechar);
  213.                 Quit();
  214.             }
  215.             sprintf(msg,"Can't find the next line in old file %s !!! :\n \"%s\"",MainFile,MsgErrorEnd);
  216.             DisplayMsg(msg);
  217.             FreeMemory(msg);
  218.             fclose(file);
  219.             FreeMemory(Filechar);
  220.             Quit();
  221.         }
  222.         ctmp=*str1;
  223.         *str1='\0';
  224.         fprintf(file,"%s",str+strlen(begin));
  225.         *str1=ctmp;
  226.         return str1;
  227.     }
  228.     else
  229.     {
  230.         fprintf(file,"%s",str+strlen(begin));
  231.         return NULL;
  232.     }
  233. }
  234.  
  235. /****************************************************************************************************************/
  236. /****                                                                                                         ****/
  237. /**                                            Indent                                                               **/
  238. /****                                                                                                         ****/
  239. /****************************************************************************************************************/
  240.  
  241. void Indent(FILE *file,int nb)
  242. {
  243.     int     i;
  244.  
  245.     for(i=0;i<nb;i++)
  246.     fprintf(file, "\t");
  247. }
  248.  
  249. /****************************************************************************************************************/
  250. /****                                                                                                         ****/
  251. /**                                            extract_dir                                                           **/
  252. /****                                                                                                         ****/
  253. /****************************************************************************************************************/
  254.  
  255. void extract_dir( char *filename )
  256. {
  257.     if (*filename)
  258.     {
  259.         filename=PathPart(filename);
  260.         *filename='\0';
  261.     }
  262. }
  263.  
  264. /****************************************************************************************************************/
  265. /****                                                                                                         ****/
  266. /**                                            extract_file                                                       **/
  267. /****                                                                                                         ****/
  268. /****************************************************************************************************************/
  269.  
  270. void extract_file( char * path, char * filename )
  271. {
  272.     strcpy(filename,FilePart(path));
  273. }
  274.  
  275. /****************************************************************************************************************/
  276. /****                                                                                                         ****/
  277. /**                                            add_extend                                                           **/
  278. /****                                                                                                         ****/
  279. /****************************************************************************************************************/
  280.  
  281. void add_extend( char *filename, char * extend )
  282. {
  283.     strcat(filename,extend);
  284. }
  285.  
  286. /****************************************************************************************************************/
  287. /****                                                                                                         ****/
  288. /**                                            remove_extend                                                       **/
  289. /****                                                                                                         ****/
  290. /****************************************************************************************************************/
  291.  
  292. void remove_extend( char *filename )
  293. {
  294.     char *aux;
  295.  
  296.     aux = strrchr(FilePart(filename),'.');
  297.     if (aux) *aux='\0';
  298. }
  299.  
  300. /****************************************************************************************************************/
  301. /****                                                                                                         ****/
  302. /**                                            change_extend                                                       **/
  303. /****                                                                                                         ****/
  304. /****************************************************************************************************************/
  305.  
  306. void change_extend( char *filename, char * extend )
  307. {
  308.     remove_extend(filename);
  309.     add_extend(filename,extend);
  310. }
  311.  
  312. /****************************************************************************************************************/
  313. /****                                                                                                         ****/
  314. /**                                            LoadFileInRam                                                       **/
  315. /****                                                                                                         ****/
  316. /****************************************************************************************************************/
  317.  
  318. /* Read a file and load it in memory */
  319. char * LoadFileInRAM(char *file,BOOL Quiet)
  320. {
  321.     BPTR                       TMPfile;
  322.     __aligned struct FileInfoBlock      Info;
  323.     char                       *adr_file = NULL;
  324.     int                        size;
  325.  
  326.     if (TMPfile = Open(file, MODE_OLDFILE))
  327.     {
  328.         ExamineFH(TMPfile, &Info);
  329.         size = Info.fib_Size;
  330.         if (!(adr_file = AllocMemory(size+1)))
  331.         {
  332.             Close(TMPfile);
  333.             return NULL;
  334.         }
  335.         Read( TMPfile, adr_file, size);
  336.         adr_file[size] = '\0';
  337.         Close(TMPfile);
  338.         return adr_file;
  339.     }
  340.     else
  341.     {
  342.         if (!Quiet)
  343.         {
  344.             char    *msg;
  345.  
  346.             if (!(msg=AllocMemory(12+strlen(file)+1)))
  347.                 return NULL;
  348.  
  349.             sprintf(msg,"Can't open %s\n",file);
  350.             DisplayMsg(msg);
  351.             FreeMemory(msg);
  352.         }
  353.         return NULL;
  354.     }
  355. }
  356.  
  357. /****************************************************************************************************************/
  358. /****                                                                                                         ****/
  359. /**                                            GetCurrentDirectory                                                   **/
  360. /****                                                                                                         ****/
  361. /****************************************************************************************************************/
  362.  
  363. char * GetCurrentDirectory(void)
  364. {
  365.     BPTR     lock;
  366.     UWORD     len                 = 512;
  367.     char     *CurrentDirectory     = NULL;
  368.  
  369.     if (!(lock = Lock("PROGDIR:",ACCESS_READ)))
  370.     {
  371.         DisplayMsg("ERROR !!!!! CAN'T OBTAIN A LOCK TO THE CURRENT DIRECTORY !!!\n");
  372.         return NULL;
  373.     }
  374.     do
  375.     {
  376.         if (CurrentDirectory)
  377.             FreeMemory(CurrentDirectory);
  378.         if (!(CurrentDirectory = AllocMemory(len+1)))
  379.         {
  380.             UnLock(lock);
  381.             return NULL;
  382.         }
  383.         NameFromLock(lock,CurrentDirectory,len);
  384.         len*=2;
  385.     }while(IoErr()==ERROR_LINE_TOO_LONG);
  386.     UnLock(lock);
  387.     return CurrentDirectory;
  388. }
  389.  
  390. /****************************************************************************************************************/
  391. /****                                                                                                         ****/
  392. /**                                            CopyFile                                                           **/
  393. /****                                                                                                         ****/
  394. /****************************************************************************************************************/
  395. BOOL CopyFile(char *FromFile,char *ToFile)
  396. {
  397.     char                            *buff;
  398.     char                            *msg;
  399.     BPTR                            File;
  400.     __aligned struct FileInfoBlock  Info;
  401.  
  402.     if (!(File = Open(FromFile,MODE_OLDFILE)))
  403.     {
  404.         if (!(msg = AllocMemory(strlen(FromFile)+1+13)))
  405.         {
  406.             return FALSE;
  407.         }
  408.         sprintf(msg,"Can't find \"%s\"",FromFile);
  409.         DisplayMsg(msg);
  410.         FreeMemory(msg);
  411.         return FALSE;
  412.     }
  413.     ExamineFH(File, &Info);
  414.     if (!(buff = AllocMemory(Info.fib_Size)))
  415.     {
  416.         Close(File);
  417.         return FALSE;
  418.     }
  419.     Read(File,buff,Info.fib_Size);
  420.     Close(File);
  421.  
  422.     if (!(File = Open(ToFile,MODE_NEWFILE)))
  423.     {
  424.         if (!(msg = AllocMemory(strlen(ToFile)+1+13)))
  425.         {
  426.             return FALSE;
  427.         }
  428.         sprintf(msg,"Can't write \"%s\"",ToFile);
  429.         DisplayMsg(msg);
  430.         FreeMemory(msg);
  431.         return FALSE;
  432.     }
  433.     Write(File,buff,Info.fib_Size);
  434.     FreeMemory(buff);
  435.     Close(File);
  436.     return TRUE;
  437. }
  438.